package com.heying.mybatis.mapper;

import com.heying.mybatis.entity.Student;
import com.heying.mybatis.entity.Teacher;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

@Mapper
public interface StudentMapper {

//    @Select("select * from student")
    List<Student> queryAll();

//    @Select("select * from student where id=#{id}")
    Student queryById(Integer id);

//    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
//    @Insert("insert into student values(null,#{stuno},#{name}," +
//            "#{birthday},#{country},#{hobby},#{sex},1)")
    void insertOne(Student student);

//    @Update("update student set stuno=#{stuno},name=#{name},birthday=#{birthday}," +
//            "country=#{country},hobby=#{hobby},sex=#{sex},tid=#{tid} where id=#{id}")
    void updateOne(Student student);

//    @Delete("delete from student where id=#{id}")
    void delOne(Integer id);

    Student queryOneAndTeacherN1(Integer id);

    @Select("select * from student")
    @Results(id="stuTeacherN1RM", value={
            //多对一
            @Result(column="tid", property="teacher", javaType= Teacher.class,
                    one=@One(fetchType = FetchType.EAGER,
                            select="com.heying.mybatis.mapper.TeacherMapper.findById"))
    })
    List<Student> queryAllAndTeacherN1();

    @Select("select * from student where tid=#{tid}")
    List<Student> findByTid(Integer tid);

    List<Student> queryAllAndTeacher();

    //只更新不为null的属性
    void updateIfNecessary(Student student);

    void deleteSome(Integer[] ids);

    //动态条件查询
    List<Student> querySomeAndTeacherByCondition(Student condition);
}